using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_YCodeGrid : Controls.DataManagerYCodeControl
{
    public event EventHandler ItemSelected;

    protected override void  ItemChanged()
    {
        // nothing to bind
    }

    private DAL.YCode GetSelectedItem(object o)
    {
        long id;
        if (o != null && Int64.TryParse(o.ToString(), out id))
        {
            return DAL.YCodes.Item(id);
        }
        else
        {
            return null;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Refresh();
        }
    }

    public void Refresh()
    {
        gridItems.DataSource = DAL.YCodes.All();
        gridItems.DataBind();
    }

    protected void Move(object sender, CommandEventArgs e)
    {
        DAL.YCode myItem = GetSelectedItem(e.CommandArgument);
        if (myItem != null)
        {
            switch (e.CommandName.ToLower())
            {
                case "up":
                    DAL.YCodes.Move(myItem, DAL.Direction.Up);
                    break;
                default:
                    DAL.YCodes.Move(myItem, DAL.Direction.Down);
                    break;
            }
        }
        Refresh();
    }

    protected void SelectItem(object sender, CommandEventArgs e)
    {
        CurrentYCode = GetSelectedItem(e.CommandArgument);
        if (CurrentYCode != null && ItemSelected != null)
        {
            ItemSelected(this, new EventArgs());
        }
    }

    protected void DeleteItem(object sender, CommandEventArgs e)
    {
        DAL.YCode myYCode = GetSelectedItem(e.CommandArgument);
        if (myYCode != null)
        {
            DAL.YCodes.Remove(myYCode);
            Refresh();
        }
    }
}
